(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.dataPatch.createVerifyController', createVerifyController);
	createVerifyController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'dataPatchService' ];
	function createVerifyController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, dataPatchService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		var log = $scope.loggerFactory.getLogger("dataPatchCreateVerify");
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.dataPatch = {};
		ctrl.dataPatchOld = {};
		
		ctrl.init = function() {
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.dPatchParms.tableConfigs.table);
			ctrl.innerGridOptions.data = 'vm.dataPatch.dPatchParms';
			
			authService.createVerify(params).then(function(response) {
				dataPatchService.initOptions(ctrl).then(function(result){
					if (response.data && response.data.model && response.data.task) {
						ctrl.dataPatch = response.data.model;
						ctrl.dataPatch.taskId = response.data.task.taskId;
						ctrl.dataPatch.dPatchParms = ctrl.dataPatch.dPatchParms || [];	
					}
				});
			});
		};
		
		ctrl.isShow = function (){
			return true;
		};
		
		ctrl.verifierPreview = function (){
			//DML cooperation in database
			ctrl.dataPatch.finalSqlScript = ctrl.dataPatch.finalSqlScript;
				ctrl.dataPatch.previewVTime = ctrl.dataPatch.previewVTime;
				return authService.verifierPreview(ctrl.dataPatch).then(checkSuccessful, checkFailed);
		};
		
		function checkSuccessful(response) {
			ctrl.dataPatch.previewVResult = response.data.message;
			ctrl.dataPatch.executeResult = response.data.message;
			ctrl.dataPatch.previewVTime = response.data.previewTime;
			ctrl.dataPatch.executeTime = response.data.previewTime;
			
		}

		function checkFailed(response) {
			ctrl.dataPatch.previewVResult = null;
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		ctrl.createVerifyApprove = function() {
			if (ctrl.dataPatch.previewVResult != null && ctrl.dataPatch.previewVTime != null){				
				modalService.openConfirmDialogs({
					'messages' : [{msg : 'title.dialog.confirm.approve', args : [ctrl.dataPatch.primaryKey]} ],
					'btnCallback' : {					
						'ok' : function() {
							authService.createVerifyApprove(ctrl.dataPatch).then(approveSuccessful, rejectFailed);
						}
					}
				});
			} else {
				modalService.openErrorDialogs({
					'messages' : [ "js.info.common.verifier.should-preview-button"]
				});
			}
		};

		ctrl.createVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.dataPatch.comments = comments;
						authService.createVerifyReject(ctrl.dataPatch).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.datapatch.list',
					'state' : 'cda.dataPatch.list',
					'params' : {}
				}]
			});
		}

		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.datapatch.list',
					'state' : 'cda.dataPatch.list',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);
